//
//  UserProfileVC.m
//  Water
//
//  Created by yh on 2017/12/12.
//  Copyright © 2017年 chenguibang. All rights reserved.
//

#import "UserProfileVC.h"
#import "TodayStatisticsController.h"
#import "WaterConversationViewController.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import <BlocksKit/BlocksKit+UIKit.h>
@interface UserProfileVC ()

@end

@implementation UserProfileVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setupView];
}

- (void)setupView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    UIView *headerContentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    self.userProfileHeaderView = WaterXIB(@"UserProfileHeaderView");
    self.userProfileHeaderView.name.text = self.waterCenterUser.nickname;
    [self.userProfileHeaderView.icon sd_setImageWithURL:[NSURL URLWithString:self.waterCenterUser.avatar]];
    [headerContentView addSubview:self.userProfileHeaderView];
    [self.userProfileHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(headerContentView);
    }];
    self.tableView.tableHeaderView = headerContentView;
    [self.userProfileViewModel getMenus];
    [self.tableView reloadData];
    
    UIBarButtonItem *addRoom =   [[UIBarButtonItem alloc] bk_initWithImage:[UIImage imageNamed:@"profile_info"] style:UIBarButtonItemStyleDone handler:^(id sender) {
            WaterConversationViewController *chat = [[WaterConversationViewController alloc] initWithConversationType:ConversationType_PRIVATE
                                                                                                             targetId:self.waterCenterUser.userId];
        
            chat.title = self.waterCenterUser.nickname;
            //显示聊天会话界面
            chat.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:chat animated:YES];
    }];
    
    self.navigationItem.rightBarButtonItem = addRoom;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.userProfileViewModel.tableViewDatas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.userProfileViewModel.tableViewDatas[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    cell.textLabel.text = self.userProfileViewModel.tableViewDatas[indexPath.section][indexPath.row][@"title"];
    cell.detailTextLabel.text = self.userProfileViewModel.tableViewDatas[indexPath.section][indexPath.row][@"value"];
    
    
    RecordsParam *param = [[RecordsParam alloc]init];
    param.userid =  self.userId;
    [WaterAPI recordsWith:param progress:nil success:^(ApiResponse *response) {
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TodayStatisticsController *todayStatisticsController =[TodayStatisticsController new];
    todayStatisticsController.userId = self.userId;
    todayStatisticsController.title = @"今日记录";
    todayStatisticsController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:todayStatisticsController animated:YES];
}



- (UserProfileViewModel *)userProfileViewModel{
    if (!_userProfileViewModel) {
        _userProfileViewModel = [[UserProfileViewModel alloc]init];
    }
    return _userProfileViewModel;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
